package com.map.station;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.database.Cursor;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Bundle;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.OverlayItem;

public class CustomMarker extends MapActivity{
	
	private MapView mapView;
	private MapController mapControl;
	private static boolean satellite, streetView, traffic;
	
	private static Intent intent;
	private static Bundle bundle = new Bundle();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mapview);
		
		// get value Satellite, StreetView, Traffic on google map
		satellite = OptionPreference.setSatellite(getBaseContext());
		streetView = OptionPreference.setStreetView(getBaseContext());
		traffic = OptionPreference.setTraffic(getBaseContext());
		
		//get "searchkeyword" from search page
		final Bundle bundle = this.getIntent().getExtras();
		String rate = bundle.getString("searchKeyword");
		
		initMapView();
		initMyLocation();
		drawStation(rate);
		
	}
	
	// set Satellite, StreetView, Traffic
	private void initMapView() {
		mapView = (MapView)findViewById(R.id.mapView);
		mapControl = mapView.getController();
		mapView.setSatellite(satellite);
		mapView.setStreetView(streetView);
		mapView.setTraffic(traffic);
		mapView.setBuiltInZoomControls(true);
	}
	
	// get and mark point to current location
	private void initMyLocation() {
		final MyLocationOverlay overlay = new MyLocationOverlay(this, mapView);
		overlay.enableMyLocation();
		overlay.runOnFirstFix(new Runnable() {
			
			@Override
			public void run() {
				mapControl.setZoom(14);
				mapControl.animateTo(overlay.getMyLocation());
			}
		});
		mapView.getOverlays().add(overlay);
	}
	
	String param;
	
	// draw image station on google map
	public void drawStation(String param) {
		Drawable cmark = null;
		this.param = param;
		
		//check user to choose station
		if(param.equalsIgnoreCase("caltex")) {
			cmark = getResources().getDrawable(R.drawable.caltex);
		} else if(param.equalsIgnoreCase("shell")) {
			cmark = getResources().getDrawable(R.drawable.shell);
		} else if(param.equalsIgnoreCase("esso")) {
			cmark = getResources().getDrawable(R.drawable.esso);
		} else if(param.equalsIgnoreCase("ptt")) {
			cmark = getResources().getDrawable(R.drawable.ptt);
		} else if(param.equalsIgnoreCase("bcp")) {
			cmark = getResources().getDrawable(R.drawable.bcp);
		}
		
		cmark.setBounds(0, 0, cmark.getIntrinsicWidth(), cmark.getIntrinsicHeight());
		MarkAdapter mad = new MarkAdapter(cmark,param);
		mapView.getOverlays().add(mad);
	}
	
	// get value for mark station on google map
	public class MarkAdapter extends ItemizedOverlay {
		
		private List<OverlayItem> location = new ArrayList<OverlayItem>();
		private Drawable marker;
		private String param;
		private Cursor c;
		public MarkAdapter(Drawable marker, String param) {
			super(marker);
			this.marker = marker;
			this.param = param;
			
			// query data from DB
			DBAdapter db = new DBAdapter(getBaseContext());
			db.open();
			c = db.getStation(param);
			if(c.moveToFirst()) {
				do {
					displayStation(c);
				} while(c.moveToNext());
			}
			db.close();
		}
		
		@Override
		public void draw(Canvas canvas, MapView mapView, boolean shadow) {
			super.draw(canvas, mapView, shadow);
			boundCenterBottom(marker);
		}

		@Override
		protected OverlayItem createItem(int i) {
			return location.get(i);
		}

		@Override
		public int size() {
			return location.size();
		}	
		
		//when tap on screen will be "Intent" to show detail
		@Override
		protected boolean onTap(int index) {
			if(param.equalsIgnoreCase("ptt")) {
				param = "sptt";
			}
			intent = new Intent(getBaseContext(), DetailActivity.class);
			bundle.putString("stationKeyword", param);
			bundle.putInt("indexKeyword", index);
			intent.putExtras(bundle);
			startActivity(intent);
			return false;
		}
		
		//convert latitude and longitude to Double
		public void displayStation(Cursor c) {
			double clat = Double.parseDouble(c.getString(2));
			double clng = Double.parseDouble(c.getString(3));
			
			GeoPoint station = new GeoPoint((int)(clng*1000000)
					,(int)(clat*1000000));

			location.add(new OverlayItem(station, "station", "station"));
			populate();
		}
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
}
